import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/common/user_singleton.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_list_entity.dart';
import 'package:xiongan_app_yy/net/internet_api.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/search_widget.dart';

class NorderAuditResultSearch extends StatefulWidget {
  int index;

  NorderAuditResultSearch(this.index);

  @override
  _NorderAuditResultSearchState createState() =>
      _NorderAuditResultSearchState();
}

class _NorderAuditResultSearchState extends State<NorderAuditResultSearch> {
  String title;
  InvListEntity bean;
  int size = 0;

  @override
  Widget build(BuildContext context) {
    title = widget.index == 0 ? "请输入入库任务单号" : "请输入出库任务单号";
    return SearchWidget(
      tipsText: title,
      onTapFuns: (value) {
        if (value != null && value.toString().length > 0) {
          if (widget.index == 0) {
            searchInTask(value);
          } else {
            searchOutTask(value);
          }
        } else {
          TostUtil.showToast(title);
        }
      },
      child: LoadBuilder(
        visibility: false,
        emptyVisibility: size == 0,
        emptyTextVisibility: size == 0,
        textEmptyTips: "暂无内容",
        builder: ListView.separated(
            itemBuilder: (BuildContext context, int index) {
              return TextCartItem(
                data: bean.datas,
                index: index,
              );
            },
            separatorBuilder: (BuildContext context, int index) =>
                Divider(height: 1, color: Colors.transparent),
            itemCount: size),
      ),
    );
  }

  void searchInTask(value) {
    InternetAPI.getInstance()
        .get("OMS/MiWarehouseInTask/GetAllMiWarehouseInTask", (data) {
      setState(() {
        bean = InvListEntity.fromJson(data);
        size = bean.datas.length;
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'userId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      'pageNo': -1,
      'appointmentmode': 10,
      'taskno': value,
    });
  }

  void searchOutTask(value) {
    InternetAPI.getInstance()
        .get("OMS/MiWarehouseOutTask/GetAllMiWarehouseOutTask", (data) {
      bean = InvListEntity.fromJson(data);
      size = bean.datas.length;
      setState(() {
      });
    }, params: {
      'operaterUserId': DataSingleton().Data.id,
      'userId': DataSingleton().Data.id,
      'tenantId': DataSingleton().Data.tenantId,
      'pageNo': -1,
      'taskno': value,
      'appointmentmode': 10,
    });
  }
}

class _Text extends Text {
  TextStyle style;
  _Text(String data,
      {this.style = const TextStyle(color: Color(0xff646464), fontSize: 14)})
      : super(data, style: style);
}

class TextCartItem extends StatelessWidget {
  final List<InvListData> data;
  final int index;
  final Function(int index) onTapFuns;

  Widget stateWidget;

  TextCartItem(
      {Key key, @required this.data, @required this.index, this.onTapFuns})
      : super(key: key);

  Widget redBox=DecoratedBox(
    decoration: BoxDecoration(color:  Color(0xffcccccc)),
  );

  @override
  Widget build(BuildContext context) {
    if (data.elementAt(index).state == "未分配") {
      stateWidget = Text(
        "未审核",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF00A5F1)),
      );
    } else if (data.elementAt(index).state == "未审批") {
      stateWidget = Text(
        "未审核",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF00A5F1)),
      );
    } else if (data.elementAt(index).state == "未入库" || data.elementAt(index).state == "未出库") {
      stateWidget = Text(
        "审核通过",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF2FC480)),
      );
    } else if (data.elementAt(index).state == "已入库" || data.elementAt(index).state == "已出库") {
      stateWidget = Text(
        "审核通过",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF2FC480)),
      );
    } else if (data.elementAt(index).state == "待执行") {
      stateWidget = Text(
        "审核通过",
        style: TextStyle(fontSize: 14.0, color: Color(0xFF2FC480)),
      );
    }else if (data.elementAt(index).state == "已驳回") {
      stateWidget = Text(
        "未审核",
        style: TextStyle(fontSize: 14.0, color: Color(0xFFFB4747)),
      );
    }else if (data.elementAt(index).state == "已废弃") {
      stateWidget = Text(
        "已废弃",
        style: TextStyle(fontSize: 14.0, color: Color(0xFFFB4747)),
      );
    }
    return GestureDetector(
      child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
            //解决GestureDetector Widget事件触发面问题
              border: Border.all(color: Colors.transparent, width: 1)),
          child: Column(
            children: <Widget>[
              Align(
                alignment: Alignment.bottomLeft,
                child: _Text("${data.elementAt(index).projectname}"),
              ),
              Container(
                padding: EdgeInsets.only(top: 10, bottom: 10),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: _Text("${data.elementAt(index).taskno}"),
                ),
              ),
              Column(
                children: <Widget>[
                  Flex(
                    direction: Axis.horizontal,
                    children: <Widget>[
                      Expanded(
                          flex: 1,
                          child: stateWidget
                      ),
                      Expanded(
                          flex: 2,
                          child: Text(
                            "${data.elementAt(index).creationTime}",
                            textAlign: TextAlign.right,
                            style: TextStyle(
                              color: Color(0xff646464),
                              fontSize: 14,
                            ),
                          )
                      ),
                    ],
                  ),
                ],
              ),
              Offstage(offstage: data.elementAt(index).remark==null,child: Column(
                  children: <Widget>[
                    ConstrainedBox(
                      constraints: BoxConstraints(
                          minWidth: double.infinity, //宽度尽可能大
                          minHeight: 40.0 //最小高度为50像素
                      ),
                      child: Container(
                        decoration: BoxDecoration(
                            color: Color(0xFFE9E9E9),
                            borderRadius: BorderRadius.circular(20)
                        ),
                        margin: EdgeInsets.only(top: 10.0), //容器外补白
                        padding:  EdgeInsets.all(5.0), //容器外补白
                        child: Text(
                            "原因:${data.elementAt(index).remark}",
                            style: TextStyle(
                              color: Color(0xff646464),
                              fontSize: 14,
                            )),
                      ),
                    )
                  ]
              ),)

            ],
          )),
      onTap: () {
        if (onTapFuns != null) {
          print("加载中~~~~");
        }
      },
    );
  }
}